package cn.bugstack.gateway.session;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.concurrent.Callable;

public class SessionServer implements Callable<Channel> {

    private  final Logger LOGGER = LoggerFactory.getLogger(SessionServer.class);

    private final EventLoopGroup boosGroup = new NioEventLoopGroup(1);

    private final EventLoopGroup workGroup = new NioEventLoopGroup();

    private Channel channel;

    /**
     * 以线程的方式实现Netty调用
     *
     * @return
     * @throws Exception
     */
    @Override
    public Channel call() throws Exception {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(boosGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .childHandler(new SessionChannelInitializer());
        ChannelFuture channelFuture = bootstrap.bind(new InetSocketAddress(8086)).syncUninterruptibly();
        this.channel = channelFuture.channel();
        return channel;
    }
}
